﻿
using Autofac.Extensions.DependencyInjection;
using Coldairarrow.Util;
using Colder.Logging.Serilog;
using EFCore.Sharding;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;

namespace Coldairarrow.Api
{
    public class Program
    {
        public static void Main(string[] args)
        {
            Host.CreateDefaultBuilder(args)
                .ConfigureLoggingDefaults()
                .UseIdHelper()
                .UseCache()
                .ConfigureServices((hostContext, services) =>
                {
                    services.AddFxServices();
                    services.AddAutoMapper();
                   
                    services.AddEFCoreSharding(config =>
                    {
                        var dbOptions = hostContext.Configuration.GetSection("Database:BaseDb").Get<DatabaseOptions>();

                        config.UseDatabase(dbOptions.ConnectionString, dbOptions.DatabaseType);
                
                        config.SetEntityAssemblies(GlobalData.AllFxAssemblies.ToArray());

                        /////////2023-8-4新增加
                        //string conName = hostContext.Configuration["ConnectionName"];
                        //if (hostContext.Configuration["LogicDelete"].ToBool())
                        //    config.UseLogicDelete();
                        //config.UseDatabase(hostContext.Configuration.GetConnectionString(conName), hostContext.Configuration["DatabaseType"].ToEnum<DatabaseType>());
                        //////////

                    });
                })
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                       // .UseRedisTwo(); 
                    webBuilder.UseUrls("http://*:5015");
                })
                 .ConfigureServices(option =>
                 {
                    // option.AddHostedService<StudyProcessBackgroundService>();
                     //option.AddHostedService<TimedBackgroundService>();
                     //需要MQTT和KafKa时
                     //option.AddHostedService<MQTTTimedExecutService>();
                     //option.AddHostedService<KafKaTimedExecutService>();
                     //option.AddHostedService<WebSocketMQTTTimedExecutService>();

                 })
                 .UseServiceProviderFactory(new AutofacServiceProviderFactory())
                .Build()
                .Run();
        }

    }
}
